package com.atguigu.lease.web.admin.custom.converter;

import com.atguigu.lease.model.enums.ItemType;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

/**
 * @author Admin
 * @title: StringToItemTypeConverter
 * @projectName lease
 * @description: StringToItemTypeConverter
 * @date 2025/1/29 16:25
 */
@Component
public class StringToItemTypeConverter implements Converter<String, ItemType> {
    @Override
    public ItemType convert(String code) {

        for (ItemType itemType : ItemType.values()) {
            //valueOf 根据传入的名称 返回对应类型  values返回枚举类的所有实例对象
            if (itemType.getCode().equals(Integer.valueOf(code))) {
                return itemType;
            }
        }

        // ItemType.class.getEnumConstants();  等价于ItemType.values()

        //if("1".equals(code)){
        //    return ItemType.APARTMENT;
        //}
        //if("2".equals(code)){
        //    return ItemType.ROOM;
        //}
        throw new IllegalArgumentException("code非法");
    }
}
